<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 产品列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>-->
                产品名称:
                <el-input
                    v-model="query.name"
                    placeholder="产品名称"
                    class="handle-input mr10"
                    @change="handleSearch"
                ></el-input>公司:
                <el-select v-model="company_id" placeholder="请选择">
                    <el-option label="全部" value=''></el-option>
                    <el-option
                        v-for="(item, index) in companyist"
                        :key="index"
                        :label="item.companyName"
                        :value="item.companyId"
                    ></el-option>
                </el-select>当前状态：
                <el-select
                    v-model="product_status"
                    placeholder="全部"
                    clearable
                    @clear="clear"
                    class="handle-select mr10"
                >
                    <el-option key="99" label="全部" value=""></el-option>
                    <el-option key="0" label="预备中" value="0"></el-option>
                    <el-option key="1" label="显示中" value="1"></el-option>
                    <el-option key="2" label="已下架" value="2"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <div class="add-more" style="display:block;">
                    <el-button style="margin:10px 0px;" type="primary" @click="addProduct">+ 新增产品</el-button>
                </div>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="index" label="序号" width="70" align="center"></el-table-column>
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <!-- <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column> -->
                <el-table-column prop="companyName" label="公司名" width="200" align="center"></el-table-column>
                <el-table-column label="产品图" width="150" align="center">
                    <template slot-scope="scope">
                       
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.productResource"
                            :preview-src-list="[scope.row.productResource]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="productName" label="产品名称"  align="center"></el-table-column>
                <!-- <el-table-column prop="productCount" label="产品数" width="150"></el-table-column> -->
                <!-- <el-table-column prop="endTime" label="闭展时间" width="150"></el-table-column> -->
                <!-- <el-table-column label="账户余额">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>-->

                <!-- <el-table-column prop="exhibitStatus" label="当前状态" width="250"></el-table-column> -->
                <el-table-column label="当前状态" width="80" align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.productStatus == 0">预备中</span>
                        <span v-else-if="scope.row.productStatus == 1">显示中</span>
                        <span v-else>已下架</span>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="发布时间" width="250" align="center"></el-table-column>
                <!-- <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.state==='成功'?'success':(scope.row.state==='失败'?'danger':'')"
                        >{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column>-->

                <!-- <el-table-column prop="date" label="注册时间"></el-table-column> -->
                <el-table-column label="操作" width="400" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="info"
                            @click="toDetail(scope.$index, scope.row)"
                        >{{scope.row.productStatus==1?'下架':'上架'}}</el-button>
                        <el-button type="primary" @click="checkProduct(scope.$index, scope.row)">查看</el-button>
                        <el-button type="success" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background  
                    maxentries:='100'
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

         <!-- 产品查看弹出框 -->
        <el-dialog title="产品详细资料" :visible.sync="productVisible" width="50%">
            <el-form ref="form" :model="productData" label-width="120px">
                <el-form-item label="产品名称">
                    <el-input v-model="productData.productName" style="width:250px;" disabled></el-input>
                </el-form-item>
                <el-form-item label="产品图片或视频" >
                    <video :src="productData.productResource" v-if="isVideo" controls  style="width:400px;" ></video>
                    <el-image
                    v-else
                        style="width:400px;"
                        class="check_image"
                        :src="productData.productResource"
                        :preview-src-list="[productData.productResource]"
                    ></el-image>
                </el-form-item>
                <el-form-item label="产品介绍">
                    <el-input
                        type="textarea"
                        rows="4"
                        v-model="productData.productDetailIntro"
                        style="width:350px;"
                        disabled
                    ></el-input>
                </el-form-item>
                <!-- <el-form-item label="产品视频">
                     <div class="video-tem" style="position:relative;">
                        <div id="J_prismPlayer" class="prism-player"></div>
                    </div>
                </el-form-item> -->
                <el-form-item label="产品简介">
                    <el-input
                        type="textarea"
                        rows="6"
                        v-model="productData.productIntroduce"
                        style="width:450px;"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input v-model="productData.createTime" style="width:250px;" disabled></el-input>
                </el-form-item>
                <el-form-item label="产品富文本">
                    <div v-html="productData.richText"></div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="checkVisible = false">取 消</el-button>
                <!-- <el-button type="primary" @click="saveEdit(form)">确 定</el-button> -->
            </span>
        </el-dialog>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address" disabled></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <!-- <el-button type="primary" @click="saveEdit(form)">确 定</el-button> -->
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
import { log } from 'util';
import { getCompanyistApi } from '../../api/productList';
//import {getCompanyList,dele teCompany} from '../../api/company'
export default {
    name: 'product',
    data() {
        return {
            // 公司列表
            companyist: [],
            company_id: '',
            // 筛选状态
            product_status: '',
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10,
                company: '',
                type: ''
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            productVisible: false,
            productData: {},
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            searchData: [],
            // 判断返回的数据是不是视频
            isVideo:''
        };
    },
    created() {
        console.log('产品列表');
        this.getCompanyist();
        this.getproductList();
        this.getCompany_id();
    },
    mounted() {
        // this.getData();
    },
    methods: {
        getCompany_id() {
            if (this.$route.query.companyId) {
                // 公司页跳过来的时候
                
                // this.company_id = this.$route.query.companyId;
                // this.handleSearch();
            }
        },
        // 获取产品列表
        getCompanyist() {
            axios.get(this.$myUrl.BASEURL + 'company/getCompanyList?pageNum=0&pageSize=0').then((res) => {
                // console.log(res);
                this.companyist = res.data.data;
            });
        },
        clear() {
            this.query.programaType = '';
            // this._getArticleList()
        },
        isProgramaType(type) {
            if (type == 0) {
                return '预备中';
            }
            if (type == 1) {
                return '显示中';
            }
            if (type == 2) {
                return '已下架';
            }
        },
        getproductList() {
            axios.get(this.$myUrl.BASEURL + 'product/getProductList?pageNum=' + this.query.pageIndex + '&pageSize=' + this.query.pageSize).then((res) => {
                console.log('获取产品列表', res);
                this.tableData = res.data.data;
                this.tableData.forEach((item, index) => {
                    ////console.log(item)
                    item.productResource =  this.$myUrl.IMGURL  + item.productResource;
                    //item.productStatus = this.isProgramaType(item.productStatus)
                });
                this.pageTotal = res.data.totalNumber;
                // if (res.data.totalNumber > 10) {
                //     this.pageTotal = res.data.totalNumber;
                // }
                // else {
                //     this.pageTotal = 1;
                // }
            });
        },
        addCompany() {
            this.$router.push({
                path: '/company-detail'
            });
        },
        addProduct() {
            this.$router.push({
                path: '/product-detail'
            });
            // this.$router.push({
            //     path:'/markdown'
            // })
        },
        toDetail(index, row) {
            console.log(row);
            if (row.productStatus == 1) {
                this.$confirm('确认下架吗', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.put(this.$myUrl.BASEURL + 'product/updateProductStatus?productId=' + row.productId + '&productStatus=2').then((res) => {
                        this.getproductList();
                    });
                    this.$message({
                        type: 'success',
                        message: '下架成功'
                    });
                });
            } else {
                this.$confirm('确认上架吗', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.put(this.$myUrl.BASEURL + 'product/updateProductStatus?productId=' + row.productId + '&productStatus=1').then(() => {
                        this.getproductList();
                    });
                    this.$message({
                        type: 'success',
                        message: '上架成功'
                    });
                });
            }
            // axios.put(this.$myUrl.BASEURL + 'product/updateProductStatus?productId='+row.productId+'&productStatus='+row.productStatus).then(res=>{

            // })
            // this.$router.push({
            //     path:'/company-detail',
            //     query: {
            //         companyId:companyId
            //     }
            // });
        },

        // 查看按钮获得数据
        checkProduct(index, row) {
            this.productVisible = true;
            axios.get(this.$myUrl.BASEURL + 'product/getProduct?productId=' + row.productId).then((res) => {
                console.log('看看', res);
                if (res.data.code == 1001) {
                    this.$message.error(res.data.msg);
                } else {
                    this.productData = res.data.data;
                    this.productData.productResource = this.$myUrl.IMGURL + this.productData.productResource;
                    //console.log(this.productData)
                    this.isVideo = this.productData.productResource.substring(this.productData.productResource.length-3) == 'mp4'
                }
            });
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            ////console.log(this.tableData);
            this.tableData.forEach((item, index) => {
                //console.log(item)
                //console.log(this.query.name)
                if (this.query.name == item.companyName) {
                    axios.get(this.$myUrl.BASEURL + 'product/getProduct?productId=2').then((res) => {
                        this.tableData = [];
                        //console.log(res);
                        this.tableData[this.tableData.length] = {
                            productId: res.data.data.id,
                            companyName: res.data.data.companyName,
                            productName: res.data.data.productName,
                            productResource: res.data.data.productResource,
                            createTime: res.data.data.createTime
                        };
                    });
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.query.pageIndex = 1
            // console.log(this.query.name);
            this.$set(this.query, 'pageIndex', 1);
            //this.getData();
            this.tableData = [];
            this.searchData = [];
            let isProduct_status = this.product_status === '' ? '' : `&productStatus=${this.product_status}`;
            // let isSroductName =  productName==''?'': `productName=${}`
            let isCompanyId = this.company_id == '' ? '' : `&companyId=${this.company_id}`;
            axios
                .get(
                    this.$myUrl.BASEURL + 'product/getProductList?pageNum=' +
                        this.query.pageIndex +
                        '&pageSize=' +
                        this.query.pageSize +
                        '&productName=' +
                        this.query.name +
                        isCompanyId +
                        isProduct_status
                )

                .then((res) => {
                    console.log('--------------', res);
                    this.pageTotal =   res.data.totalNumber
                    res.data.data.forEach((item, index) => {
                        item.productResource = this.$myUrl.IMGURL + item.productResource;
                        this.tableData.push(item);
                    });
                });
        },
        // 删除操作
        handleDelete(index, row) {
            console.log(row);
            let id = row.productId;
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    axios.delete(this.$myUrl.BASEURL + 'product/deleteProduct?productId=' + id).then((res) => {
                        console.log(res);
                        this.getproductList();
                    });
                    this.$message.success('删除成功');
                    this.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            console.log(index);
            console.log(row.productId);
            this.$router.push(`/product-detail?productId=${row.productId}`);
            // this.idx = index;
            this.form = row;
            // this.editVisible = true;
            //console.log(this.form)
        },

        // 保存编辑
        saveEdit() {
            //console.log(this.form)
            this.form.companyName = this.form.name;
            this.form.companyIntroduce = this.form.address;
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
            axios
                .put(this.$myUrl.BASEURL + 'company/updateCompany', {
                    id: this.form.companyId,
                    companyName: this.form.companyName,
                    companyLogo: this.form.companyLogo,
                    companyIntroduce: this.form.companyIntroduce
                })
                .then((res) => {
                    //console.log(res)
                });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getproductList();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.table .el-button--small,
.el-button--small.is-round {
    width: 20%;
    min-width: 30px;
}
.table .el-button--small,
.el-button--small.is-round {
    padding: 9px 0px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.mr11 {
    width: 200px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 80px;
    height: 80px;
    /* border-radius: 50%; */
}
</style>
